function [experiment_Index,analyteList]=SaveFolders(folderPaths, runParams, redoFileload, conn)

experiment_Index =1;


fields = {'baseline_Threshold', ...
    'num_ClusterFFT_coef', ...
    'num_peakFFT_coef', ...
    'minimum_Width', ...
    'clusterSize', ...
    'minimum_FFT_Size', ...
    'lowPass_Freq', ...
    'minimum_cluster_FFT_Size'};

field = fields{1};
whereSQL = ['WHERE Round(' field ',4)=Round(' num2str( runParams.(field)) ',4)'];
for I=2:length(fields)
    field = fields{I};
    whereSQL=[whereSQL ' AND Round(' field ',4)=Round(' num2str( runParams.(field)) ',4)']; %#ok<AGROW>
end

sql =['select folders.Folder_Index, folders.Folder from folders ' whereSQL ';'];

ret = exec(conn,sql);
ret = fetch(ret);


FolderNames=(ret.Data.Folder);
for I=1:size(folderPaths,1)
    for J=1:size(FolderNames)
        a=folderPaths{I,2};
        a=strrep(a,'\','/');
        if strcmp(a,char(FolderNames{J}))
            folderPaths{I,4}=ret.Data.Folder_Index(J);
        end
    end
end


for I=1:size(folderPaths,1)
    for J=1:size(FolderNames)
        a=folderPaths{I,3};
        a=strrep(a,'\','/');
        if strcmp(a,char(FolderNames{J}))
            folderPaths{I,5}=ret.Data.Folder_Index(J);
        end
    end
end

sql = ['select * from analytes;'];
ret = fetch(exec(conn,sql));

if isstruct(ret.Data)==false
    DoFolders=unique(folderPaths(:,1));
    for I=1:size(DoFolders,1)
        sql = ['insert into analytes (Analyte_Index, Analyte_Name, ' ...
            'num_Experiment_Samples, num_Control_Samples,numPeaks,numWaterPeaks,numClusters) values ' ...
            '(' num2str(I) ',''' DoFolders{I} ''',0,0,0,0,0);'];
        exec(conn,sql);
    end
else
    FolderNames=(ret.Data.Analyte_Name);
    cc=1;
    alreadyDone=[];
    for I=1:size(folderPaths,1)
        for J=1:size(FolderNames)
            a=folderPaths{I,1};
            a=strrep(a,'\','/');
            if strcmp(a,char(FolderNames{J}))
                alreadyDone(cc)=I;
                cc=cc+1;
            end
        end
    end
    rows=1:size(folderPaths,1);
    rows(alreadyDone)=[];
    DoFolders = folderPaths(rows,:);
    DoFolders=unique(DoFolders(:,1));
    
    m=max(ret.Data.Analyte_Index);
    
    for I=1:size(DoFolders,1)
        sql = ['insert into analytes (Analyte_Index, Analyte_Name, ' ...
            'num_Experiment_Samples, num_Control_Samples,numPeaks,numWaterPeaks,numClusters) values ' ...
            '(' num2str(m+I) ',''' DoFolders{I} ''',0,0,0,0,0);'];
        exec(conn,sql);
    end
end

sql = ['select * from analytes;'];
ret = fetch(exec(conn,sql));

FolderNames=(ret.Data.Analyte_Name);
Indexs= ret.Data.Analyte_Index;
cc=1;
alreadyDone=[];
for I=1:size(folderPaths,1)
    for J=1:size(FolderNames)
        a=folderPaths{I,1};
        a=strrep(a,'\','/');
        if strcmp(a,char(FolderNames{J}))
            alreadyDone(cc)=Indexs(J);
            folderPaths{I,6}=Indexs(J);
            cc=cc+1;
        end
    end
end

analyteList=unique(alreadyDone);

for I=1:length(analyteList)
    sql = ['delete from analytefolders where Analyte_Index=' num2str(analyteList(I)) ';'];
    exec(conn,sql);
end

for I=1:size(folderPaths,1)
    sql = ['Insert into analytefolders (Analyte_Index, Folder_Index, Control) Values (' ...
        num2str(folderPaths{I,6}) ',' num2str( folderPaths{I,4}) ',1),(' ...
        num2str(folderPaths{I,6}) ',' num2str( folderPaths{I,5}) ',0);'];
    exec(conn,sql);
end

experiment_Index=SaveExperiment(conn,runParams,analyteList);







return;


